/* Copyright 2008 Bas van den Berg
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <string>

#include "yaffut.h"

#include "myassert.h"

#define private public
#include "AliasList.h"
#undef private

using namespace std;
using namespace Chronos;


struct AliasListTest {
	AliasList list;
};


TEST(AliasListTest, testShouldAddFindRemove) {
	list.add("la", "look at");
	const string& result = list.find("la");
	ASSERT_STR_EQUAL("look at", result);

	ASSERT_EQUAL(1, list.size());
	ASSERT_TRUE(list.remove("la"));
	ASSERT_EQUAL(0, list.size());

	const string& result2 = list.find("la");
	ASSERT_STR_EQUAL("", result2);

	ASSERT_TRUE(!list.remove("la"));
}


TEST(AliasListTest, testShouldGetAll) {
	list.add("la", "look at");
	list.add("dw", "drink wine");
	list.add("ws", "wield saber");

	ASSERT_EQUAL(3, list.size());

	AliasList::AliasesConstIter iter = list.aliases.begin();

	ASSERT_STR_EQUAL("dw", iter->first);
	ASSERT_STR_EQUAL("drink wine", iter->second);
	++iter;

	ASSERT_STR_EQUAL("la", iter->first);
	ASSERT_STR_EQUAL("look at", iter->second);
	++iter;

	ASSERT_STR_EQUAL("ws", iter->first);
	ASSERT_STR_EQUAL("wield saber", iter->second);
	++iter;
	ASSERT_TRUE(iter == list.aliases.end());
}


TEST (AliasListTest, testShouldNotReplace) {
    list.add("ab", "abc");
    ASSERT_STR_EQUAL("abc", list.replace("abc"));
}


TEST(AliasListTest, testShouldReplaceWords) {
    list.add("dw", "drink wine");
    ASSERT_STR_EQUAL("drink wine", list.replace("dw"));

    list.add("beer", "wine");
    ASSERT_STR_EQUAL("drink wine", list.replace("drink beer"));

    list.add("drink", "eat");
    list.add("water", "hamburger");
    list.add("pure", "fat");
    ASSERT_STR_EQUAL("eat fat hamburger", list.replace("drink pure water"));

    ASSERT_STR_EQUAL("", list.replace(""));
    ASSERT_STR_EQUAL("   ", list.replace("   "));
    ASSERT_STR_EQUAL("safe the words", list.replace("safe the words    "));
}


TEST(AliasListTest, testShouldNotReplaceAfterAliasCommand) {
    list.add("beer", "wine");
    ASSERT_STR_EQUAL("drink wine", list.replace("drink beer"));
    ASSERT_STR_EQUAL("alias beer", list.replace("alias beer"));
    ASSERT_STR_EQUAL("unalias beer", list.replace("unalias beer"));
}


TEST(AliasListTest, testShouldSerializeAndBack) {
	list.add("la", "look at");
	list.add("dw", "drink wine");
    string text = list.toString();
    ASSERT_STR_EQUAL("dw\ndrink wine\nla\nlook at", text);

    AliasList list2(text);
    ASSERT_STR_EQUAL("look at", list2.aliases["la"]);
    ASSERT_STR_EQUAL("drink wine", list2.aliases["dw"]);
}

